package br.com.android.productivecenter;

import java.util.Calendar;

import br.com.android.R;
import br.com.android.modelo.Dialogs;
import br.com.android.persistencia.manipularDados;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class EditarCompromissoActivity extends Activity{
	
	private int mYear;
    private int mMonth;
    private int mDay;
    private int mHour;
    private int mMinute;
    private TextView mDateDisplay;
    private TextView mTimeDisplay;
    
    static final int DATE_DIALOG_ID = 0;
    static final int TIME_DIALOG_ID = 1;
    
	public String titulo;
	public String descricao;
	public String data;
	public String hora;
	
	public int editado;

	public manipularDados persistencia;
	public String userName;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);	
        
       if(getIntent().getStringExtra("userName") != null)
        	userName = getIntent().getStringExtra("userName");
        
        titulo = getIntent().getStringExtra("nome");
        descricao = getIntent().getStringExtra("descricao");
        hora = getIntent().getStringExtra("hora");
        data = getIntent().getStringExtra("data");
        
        persistencia = getIntent().getExtras().getParcelable("nelson"); 
        
        editado = 0;
        		
		CarregarTelaEditar();
    }
	
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year, 
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };
        
       private TimePickerDialog.OnTimeSetListener mTimeSetListener =
            new TimePickerDialog.OnTimeSetListener() {
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    mHour = hourOfDay;
                    mMinute = minute;
                    updateDisplay2();
                }
        };
	
	 private void updateDisplay() {
	        mDateDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mDay).append("/")
	                    .append(mMonth +1).append("/")
	                    .append(mYear).append(" "));
	    }
	 
	 private void updateDisplay2() {
	        mTimeDisplay.setText(
	            new StringBuilder()
	                    // Month is 0 based so add 1
	                    .append(mHour).append(":")
	                    .append(mMinute).append(" "));
	    }
	 
	public void CarregarTelaEditar(){
    	
    	setContentView(R.layout.editar_compromisso);
    	
    	final Calendar c = Calendar.getInstance();
	     mYear = c.get(Calendar.YEAR);
	     mMonth = c.get(Calendar.MONTH);
	     mDay = c.get(Calendar.DAY_OF_MONTH);
	     mHour = c.get(Calendar.HOUR_OF_DAY);
         mMinute = c.get(Calendar.MINUTE);
         
         EditText titulo3 = (EditText)findViewById(R.id.tituloCampoEdit);
 		 titulo3.setText(titulo);	
 		
 		//EditText dat = (EditText)findViewById(R.id.dataEdit);
 		//dat.setText(data);	
 		
 		//EditText hor = (EditText)findViewById(R.id.definirHoraEdit);
 		//hor.setText(hora);	
 		
 		EditText obs = (EditText)findViewById(R.id.observacoesCampoEdit);
 		obs.setText(descricao);	
        
	     mDateDisplay = (TextView) findViewById(R.id.dataEdit);
	     mDateDisplay.setText(data);
	     mTimeDisplay = (TextView) findViewById(R.id.definirHoraEdit);
		 mTimeDisplay.setText(hora);
		 
		Button data = (Button) findViewById(R.id.definirDataEdit);
		data.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				showDialog(DATE_DIALOG_ID);
			}
		 });
		
		Button hora = (Button) findViewById(R.id.botaoDefHoraEdit);
		hora.setOnClickListener(new View.OnClickListener() {
				
			@Override
			public void onClick(View v) {
				showDialog(TIME_DIALOG_ID);
			}
		 });	
    		
    	Button voltar = (Button) findViewById(R.id.voltarCompEdit);
    	voltar.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				
				EditText titulo3 = (EditText)findViewById(R.id.tituloCampoEdit);
				TextView dat = (TextView)findViewById(R.id.dataEdit);				
				TextView hor = (TextView)findViewById(R.id.definirHoraEdit);			
				EditText obs = (EditText)findViewById(R.id.observacoesCampoEdit);

				String Ntitulo = titulo3.getText().toString();
				String Ndescricao = obs.getText().toString();
				String Ndata = dat.getText().toString();
				String Nhora = hor.getText().toString();
				
				Intent i = new Intent(EditarCompromissoActivity.this,GoogleCalendarActivity.class);
				if(editado == 1){
					
					i.putExtra("Ndescricao",Ndescricao);
					i.putExtra("Ntitulo", Ntitulo);
					i.putExtra("Antigotitulo", titulo);
					i.putExtra("Ndata", Ndata);
					i.putExtra("Nhora",Nhora);
				}
				i.putExtra("userName",userName);
				i.putExtra("nelson", persistencia);
				startActivity(i);
				
			}
    	});	
    	
    	Button salvar = (Button) findViewById(R.id.botaoSalvarEdit);
    	salvar.setOnClickListener(new View.OnClickListener() {
		
			@Override
			public void onClick(View v) {
				
				editado = 1;
				Dialogs.imprimirMensagem(EditarCompromissoActivity.this,"Salvar Edição",
																	"Salvo com sucesso!");	
			}
    	});	
    			
	}
	 protected Dialog onCreateDialog(int id) {
  	    switch (id) {
    	    case DATE_DIALOG_ID:
    	        return new DatePickerDialog(this,
    	                    mDateSetListener,
    	                    mYear, mMonth, mDay);
    	     case TIME_DIALOG_ID:
    	    	 return new TimePickerDialog(this, 
    	    			 	mTimeSetListener, 
    	    			 	mHour, mMinute, false);
    	    }
  	    
    	    return null;
    	}

}
